package com.sise.aio和bio和nio.nio;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;

/**
 * 使用nio进行数据的写入
 * @author idea
 * @data 2019/1/20
 */
public class Test3 {
    private static byte message[] = { 83, 111, 109, 101};

    public static void main(String[] args) throws IOException {
        FileOutputStream fileOutputStream=new FileOutputStream("D:\\test.txt");

        FileChannel fileChannel=fileOutputStream.getChannel();

        ByteBuffer byteBuffer=ByteBuffer.allocate(1024);
        for(int i=0;i<message.length;i++){
            byteBuffer.put(message[i]);
        }
        byteBuffer.flip();

        fileChannel.write(byteBuffer);
        fileChannel.close();
        fileOutputStream.close();
    }

}
